#!/bin/bash

# 安装 Go
install_go() {
    if ! command -v go &> /dev/null; then
        echo "开始下载 Go 1.18"
        arch=$(uname -m)
        if [[ $arch == "x86_64" ]]; then
            wget http://10.75.2.14:8089//group1/default/20230513/15/48/2/go1.18.linux-amd64.tar.gz
        elif [[ $arch == "armv7l" || $arch == "aarch64" ]]; then
            wget http://10.75.2.14:8089//group1/default/20230513/15/48/2/go1.18.10.linux-arm64.tar.gz
        else
            echo "不支持的架构：$arch"
            exit 1
        fi
        echo "Go 下载完成"
        echo "开始解压 Go 1.18"
        sudo tar -C /usr/local -xzf go1.18*.tar.gz
        echo "Go 解压完成"
        echo "开始更新环境变量"
        sudo sh -c 'echo "export PATH=\$PATH:/usr/local/go/bin" >> /etc/profile'
        source /etc/profile
        echo "环境变量更新成功"
    else
        echo "发现 Go 环境，跳过安装"
    fi
}

# 安装 any_api_server
install_any_api_server() {
    echo "开始创建目录 /home/www"
    mkdir /home/www
    echo "创建目录 /home/www 完成"

    echo "切换到 /home/www 目录"
    cd /home/www

    echo "开始创建 any_api_server.go 文件并写入代码"
    cat << 'EOF' > any_api_server.go
    package main

    import (
        "github.com/gin-gonic/gin"
        "math/rand"
    )

    type Data struct {
        Name, Age,Address, Mobile, Money, Like, ID_card,Sex,Job,University string
        ID                                         int
    }

    func randomPath(c *gin.Context) {
        jsonData := []Data{
              {
                ID:       0,
                ID_card:  "6221881460008225489",
                Name:     "吕赐",
                Age:      "26",
                Sex:      "男",  
                Address:  "北京市朝阳区三里屯北小街甲2号院",
                Mobile:   "15001263227",
                Money:    "5000000",
                Like:     "篮球",
                Job:      "数据分析师",
                University:"西安交通大学",
            },
             {
                ID:       1,
                ID_card:  "6221881460008296587",
                Name:     "徐艺童",
                Age:      "21",
                Address:  "北京市朝阳区朝外大街甲6号国际俱乐部公寓",
                Mobile:   "15998167380",
                Money:    "300000",
                Like:     "街舞",
                Job:      "软件工程师",
                University:"西北工业大学",
            },
        }
        c.JSON(200, gin.H{
            "message": jsonData[rand.Intn(len(jsonData))],
        })
    }

    func main() {
        r := gin.Default()
        r.GET("/*any", randomPath)
        r.POST("/*any", randomPath)
        r.DELETE("/*any", randomPath)
        r.PUT("/*any", randomPath)
        r.PATCH("/*any", randomPath)
        r.OPTIONS("/*any", randomPath)
        r.Run(":80")
    }
EOF

    echo "any_api_server.go 文件创建并写入代码完成"

    echo "开始执行 go mod init api_server.go"
    go mod init api_server.go
    echo "go mod init api_server.go 执行完成"

    echo "开始配置 GOPROXY 环境变量为 https://goproxy.cn"
    go env -w GOPROXY=https://goproxy.cn
    echo "GOPROXY 环境变量配置完成"

    echo "开始安装依赖包 github.com/gin-gonic/gin"
    go get -u github.com/gin-gonic/gin
    while [ ! -f ~/go/pkg/mod/github.com/gin-gonic/gin*/go.mod ]; do sleep 1; done
    echo "依赖包 github.com/gin-gonic/gin 安装完成"

    echo "开始编译 any_api_server.go 程序"
    go build -o any_api_server any_api_server.go
    echo "编译 any_api_server.go 程序完成"

    echo "检查端口 80 是否被占用"
    if lsof -Pi :80 -sTCP:LISTEN -t >/dev/null ; then
        echo "端口 80 已被占用，杀掉占用该端口的进程"
        kill $(lsof -t -i:80)
        echo "占用端口 80 的进程已被杀掉"
    fi

    # 安装 any_api_server 服务
    echo "正在将api服务写进系统服务"
    cat <<EOF > /lib/systemd/system/any_api_server.service
    [Unit]
    Description=any_api_server service
    After=network.target

    [Service]
    Type=simple
    User=root
    Restart=always
    RestartSec=5
    ExecStart=/home/www/any_api_server &

    [Install]
    WantedBy=multi-user.target
EOF
    echo "any_api_server 服务文件已写入"

    echo "正在重新加载 systemd 配置文件..."
    systemctl daemon-reload
    systemctl enable any_api_server

    echo "正在启动服务..."
    systemctl start any_api_server

    # 等待服务启动成功
    while ! systemctl is-active --quiet any_api_server; do
        sleep 1
    done

    echo "any_api_server 服务已启动"
}

# 安装 Go
install_go

# 安装 any_api_server
install_any_api_server

# 关闭 SELinux
echo "关闭 SELinux 配置"
sudo setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
if [ $? -eq 0 ]; then
    echo "SELinux 配置已更新"
else
    echo "更新 SELinux 配置失败"
    exit 1
fi

# 设置文件句柄数限制和进程数限制
echo "开始设置句柄数"
echo "* soft nofile 204800" >> /etc/security/limits.conf
echo "* hard nofile 204800" >> /etc/security/limits.conf
echo "* soft nproc 204800" >> /etc/security/limits.conf
echo "* hard nproc 204800" >> /etc/security/limits.conf
ulimit -n 204800
ulimit -u 204800

# 检查文件句柄数限制和进程数限制是否设置成功
if grep -q "204800" /etc/security/limits.conf && ulimit -n | grep -q "204800" && ulimit -u | grep -q "204800"; then
    echo "句柄数限制和进程数限制已设置成功"
else
    echo "设置句柄数限制和进程数限制失败"
    exit 1
fi

echo "安装完成"